<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by Hbuilder.
 * User: Ason
 * Date: 15-12-11
 * Time: 上午11:28
 * 考试系统控制器
 */
class Exam extends WL_controller{
	public $exam = array();	//本场考场信息 
	public function __construct(){
		parent::__construct();
		$this->logged_check();
		//考场信息
		$this->exam = $this->exam_model->get_exam_by_cid( sess( 'cid' ) );
		//服务器时间
		$this->data[ 'time' ] = $this->rest_time();
		
		$test_paper = $this->exam_model->get_test_paper_by_sid( sess( 'sid' ) );

	}
	
	/**
	 * 考试说明
	 * */
	public function exam_tips(){

		if( $this->exam ){
			$this->data['exam'] = $this->exam;	//开考时间
			$this->data[ 'handin_time' ] = '';	//交卷时间

			$test_paper = $this->exam_model->get_test_paper_by_sid( sess('sid') );
			if( empty( $test_paper['question'] ) ){
				//生成试题
				$this->exam_model->init_test_paper( sess( 'sid' ) );
			}else{
				//获取交卷时间
				$this->data[ 'handin_time' ] = $test_paper['handin_time'];
			}
			//写入登录信息
			$this->exam_model->update_login_info( sess( 'sid' ) );
			
			$this->view( 'exam_tips' );
		}else{
			$this->redirect('无考试安排！', '你的班级暂时没有考试安排！请与任课教师核对考试信息！', 'login' );
		}
	}
	
	/**
	 * 考试页面
	 * @param $p 题目编号，0开始
	 * */
	public function main( $p = 0 ){
		
		$this->data['p'] = $p;
		//没到考试时间或考试已结束，强制退回考试说明页
		if( time() < strtotime( $this->exam['time'] ) || ( time() > strtotime( $this->exam['time'] )+ $this->data['setting']['time'] * 60 ) )
			redirect( '/exam/exam_tips' );
		//根据学号，获取考题信息
		$test_paper = $this->exam_model->get_test_paper_by_sid( sess( 'sid' ) );
		//如果已交卷，强制回到exam_tips
		if( !empty( $test_paper[ 'handin_time' ] ) )
			redirect( '/exam/exam_tips' );
		if( empty( $test_paper['question'] ) ){
			$this->exam_model->init_test_paper( sess( 'sid' ) );
			redirect( '/exam/main' );
		}
		$q = explode( ',', $test_paper['question'] );
		
		$this->data[ 'done' ] = '[' . implode( ',', unserialize( $test_paper[ 'done' ] ) ) . ']';
		
		$q_id = $q[ $p ];
		$this->data['q'] = $this->exam_model->get_q_by_id( $q_id );

		$this->view( 'main' );
	}
	
	/**
	 * 上传文件处理
	 * $_POST['p']是上传的题号，1开始
	 * */
	public function upload(){
			
		//根据学号，获取考题信息
		$test_paper = $this->exam_model->get_test_paper_by_sid( sess( 'sid' ) );
		//如果已交卷，强制回到exam_tips
		if( @$test_paper[ 'handin_time' ] )
			redirect( '/exam/exam_tips' );
		//检查、创建该考生的上传目录
		$a_dir = $this->data['setting']['a_dir'] . sess( 'sid' );
		if( !is_dir( $a_dir ) ){
			mkdir( $a_dir );
		}
		$p = $this->input->post('p');	//题号
		$config[ 'upload_path' ] = $a_dir;
		$config[ 'allowed_types' ] = $this->data['setting'][ 'allowed_types' ];
		$config[ 'file_name' ] = $p;
		$config[ 'overwrite' ] = TRUE;

		$this->load->library('upload', $config);
		//异常跳转回第一题
		if( empty($p) )
			$this->redirect( '<span class="alert alert-danger">上传异常</span>', $this->upload->display_errors(), '/exam/main/' );
        
        if ( ! @$this->upload->do_upload('file') ){
            $this->redirect( '<span class="alert alert-danger">上传失败</span>', $this->upload->display_errors(), '/exam/main/'.($p-1) );
        }else{
        	//本题完成，状态写入test_paper表
        	$this->exam_model->done_q( $p-1 );
            $this->redirect( '<span class="alert alert-success">上传成功</span>', '<h5>题目 【'.$p.'】 答案上传成功！</h5>', '/exam/main/'.($p-1) );
        }
	}
	
	/**
	 * 交卷处理
	 * @param $sid 交卷学生的学号
	 * */
	public function handin( $sid ){
		
		$json = [ 'error' => '未知错误', 'status'=>'error' ];
		//改变试卷状态
		if( $this->exam_model->handin( $sid ) ){
			$json[ 'status' ] = 'success';
			$json[ 'error' ] = '交卷成功';
		}else{
			$json[ 'error' ] = '交卷处理出错，请联系监考老师处理！';
		}
		echo json_encode( $json );return;
	}
	
	/**
	 * 返回剩余时间
	 * */
	private function rest_time(){
		
		$now = time();
		$end = strtotime( $this->exam['time'] )+ $this->data['setting']['time'] * 60;
		$rest = $end - $now;
		if( $rest >= 0 && $rest < $this->data['setting']['time'] * 60  ){
			return $rest;
		}else{
			return '-1';
		}
	}
	
	/**
	 * 中转页面
	 * @param $title 页面标题
	 * @param $content 内容
	 * @param $url 自动跳转url
	 * */
	public function redirect( $title = '', $content = '', $url = '' ){
		
		$this->data[ 'error' ][ 'title' ] = $title;
		$this->data[ 'error' ][ 'content' ] = $content;
		$this->data[ 'error' ][ 'url' ] = site_url( $url );
		$this->view( 'redirect' );
	}
}
	